<?php

class User extends Zend_Db_Table_Abstract{

	protected $_name='users';	
	protected $_primary = 'id';
	
	protected $_dependentTables = array(
		'Message',
		'InvitationResponse',
		'InvitationList',
		'AuthorizationMap',
		'UserRelation',
		'UserMessage',
		'UserFriendRequest',
		'Activity',
		'PasswordResetAuthorization',
		'UserProfile'
	);
	
	/**
	 * Find user by id.
	 * 
	 * @param $id user id
	 */
	public static function findById($id) {
		$user =  new User();
		return $user->find($id)->current();	
	}
	
    function checkEmailAlreadyTaken($email) {
		 $select = $this->select()
		  ->where('email=?',$email);

	     $result = $this->fetchRow($select);
	     
		 if($result){ return true;}
		 return false;
    }
    
    function checkUserAlreadyActivated($email) {
    	$select = $this->select()
    	->where('email=?',$email)
    	->where('status=?','active');
    	
	     $result = $this->fetchRow($select);
	     
		 if($result){ return true;}
		 return false;
    }
    
    /**
     * Find user by email.
     * 
     * @param $email
     * @return return user
     */
    public static function findByEmail($email) {
    	$user = new User();
    	$select = $user->select()
    	->where('email = ?', $email);
    	$row = $user->fetchRow($select);
    	
    	return $row;
    }

    /**
     * Add user.
     * 
     * @param $data
     * @return return created user id.
     */
    public static function addUser($data) {
    	$user = new User();
    	$id = $user->insert($data);
    	return $id;
    }
    
    /**
     * Find messages posted by this user.
     * 
     * @param $id user id
     */
    function findMessages($id) {
    	$user = $this->findById($id);
    	return $user->findDependentRowset('Message');
    }
    
    /**
     * Return if the given user has be invited in given invitation,
     * a user is defined as invited only when it in the invitation list
     * and status is active.
     * 
     * @param $user user object
     * @param $invitation_id invitation look for
     * @return return true if $user is invited in $invitation
     */
    public static function isInvited($user, $invitation) {
    	$isInvited = false;
    	$invitationList = new InvitationList();
    	$invitedInvitations =$user->findManyToManyRowset('Invitation', 'InvitationList', 'InvitedUser', 'Invitation', $invitationList->select()->where('i.status=?', 'active'));
    	foreach ($invitedInvitations as $invitedInvitation) {
    		if ($invitedInvitation->id == $invitation->id) {
    			$isInvited = true;
    			break;
    		}
    	} 
    	return $isInvited;
    }
    
    /**
     * Find all contacts by given user.
     * 
     * @param $user user
     */
    public static function getContacts($user) {
    	$userRelation = new UserRelation();
    	return $user->findManyToManyRowset('User', 'UserRelation', 'OriginalUser', 'PotentialUser', $userRelation->select()->where('i.relation_type=?',1));
    }
    
    public static function findFriendsByUser($user) {
    	
    }
    
    /**
     * Find messages posted on given user and type.
     * 
     * @param $user
     * @param $type user message type
     * @return return messages
     */
    public static function findMessagesToGivenUserAndType($user, $type) {
    	$message = new Message();
    	return $user->findManyToManyRowSet('Message', 'UserMessage', 'TargetUser', 'Message', $message->select()->where('i.type=?', $type)->order('m.created DESC'));
    }
    
	/**
     * Find all friends by given user.
     * 
     * @param $user user
     */
    public static function getFriends($user) {
    	$userRelation = new UserRelation();
    	return $user->findManyToManyRowset('User', 'UserRelation', 'OriginalUser', 'PotentialUser', $userRelation->select()->where('i.relation_type=?',2));
    }
    
    /**
     * Check if the user is friend of matchUser.
     * 
     * @param $user
     * @param $matchUser
     */
    public static function isFriend($user, $matchUser) {
    	$result = false;
    	foreach (User::getFriends($user) as $friend) {
    		if ($friend->id == $matchUser->id) {
    			$result = true;
    			break;
    		}
    	}
    	if (!$result) {
	    	foreach (User::getFriends($matchUser) as $friend) {
	    		if ($friend->id == $user->id) {
	    			$result = true;
	    			break;
	    		}
	    	}
    	}
    	return $result;
    }
    
    /**
     * Get all friend request to given user by given criteria,
     * the criteria, for example, can be find all unprocessed, all approved and so on, 
     * Order result be created date.
     * 
     * @param $user
     * @param $response
     * @return return list of users that send add friend request
     */
    public static function getFriendRequest($user, $response) {
    	$userFriendRequest = new UserFriendRequest();
    	return $user->findManyToManyRowset('User', 'UserFriendRequest', 'TargetUser', 'User', $userFriendRequest->select()->where('i.response=?', $response)->order('i.created DESC'));
    }
    
    /**
     * Find user activities by type, if type is not given, then list all activities of that user.
     * 
     * @param $user
     * @param $type activity type
     * @return return activities of given user by given activity type
     */
    public static function getActivityByType($user, $type) {
    	$activity = new Activity();
    	if (isset($type)) {
    		return $user->findDependentRowset('Activity', 'User', $activity->select()->where('type=?', $type)->order('created DESC'));
    	} else {
    		return $user->findDependentRowset('Activity', 'User', $activity->select()->order('created DESC'));
    	}
    }
    
	/**
	 * Find unapproved attend request users.
	 * 
	 * @param $invitation
	 * @param $status
	 */
    public static function getUserAttendRequestByStatus($invitation, $status) {
    	$invitationList = new InvitationList();
    	return $invitation->findManyToManyRowset('User', 'InvitationList', 'Invitation', 'InvitedUser', $invitationList->select()->where('i.status=?', $status));
    }
	
	/**
	 * Update password.
	 * 
	 * @param $passwordEncrypted
	 */
	public static function updatePassword($user, $passwordEncrypted) {
		$user->password = $passwordEncrypted;
		$user->save();
	}
	
	/**
	 * Get user detail profile by given user.
	 * 
	 * @param $user
	 * @return return user profile
	 */
	public static function getUserProfile($user) {
		return $user->findDependentRowset('UserProfile', 'User');
	}
}